import { observable, action } from 'mobx';
import API from '../assets/js/api';
import axios from 'axios';
import SockJS from 'sockjs-client';
import Stomp from "stompjs";

// 前端静态数据
const peopleData = [
    {id: 'gs', name: '甘肃', value: null},
    {id: 'xz', name: '西藏', value: null},
    {id: 'sc', name: '四川', value: 820},
    {id: 'qh', name: '青海', value: null},
    {id: 'yn', name: '云南', value: null}
]

const commodityData = [
    { id: 'pthw', name: '普通货物', value: 180 },
    { id: 'bwdzw', name: '濒危动植物', value: 462 },
    { id: 'fdxcp', name: '反动宣传品', value: 2460 },
    { id: 'gjsl', name: '贵金属类', value: 46 },
    { id: 'dp', name: '毒品', value: 22 }
]

const companyData = {
    country: [
        { id: 'America', name: '美国', value: null },
        { id: 'European', name: '欧盟', value: null },
        { id: 'Japan', name: '日本', value: null },
    ],
    region: [
        { id: 'sc', name: '四川', value: 654 },
        { id: 'gs', name: '甘肃', value: null },
        { id: 'xz', name: '西藏', value: null },
        { id: 'qh', name: '青海', value: null },
        { id: 'yn', name: '云南', value: null }
    ]
}


class HomeStore {
    @observable current: string = '';
    @observable listType: string = 'people'; // people commodity company
    @observable listDate: string = 'today'; // today yesterday week month
    @observable listTypes = [
        { id: 'people', value: '重点人员' },
        { id: 'commodity', value: '重点商品' },
        { id: 'company', value: '重点企业' }
    ];
    @observable listDates = [
        { id: 'today', value: '今日' },
        { id: 'yesterday', value: '昨日' },
        { id: 'week', value: '本周' },
        { id: 'month', value: '本月' }
    ]
    @observable listData: any = peopleData;
    @observable dataApi = API.emphasisPeopleLists;
    @observable flightData: any = [];

    @action.bound
    changeListType(listType: string) {
        this.listType = listType;
        switch (listType) {
            case 'people':
                // this.dataApi = API.emphasisPeopleLists;
                this.listData = peopleData;
                break;
            case 'commodity':
                // this.dataApi = API.emphasisCommodityLists;
                this.listData = commodityData;
                break;
            case 'company':
                // this.dataApi = API.emphasisCompanyLists;
                this.listData = companyData;
                break;
            default:
                break;
        }
        // this.getEmphasis()
    }

    @action.bound
    changeListDate(listDate: string) {
        this.listDate = listDate;
        // this.getEmphasis()
    }

    @action.bound
    getEmphasis() {
        axios.get(`${this.dataApi}?dateType=${this.listDate}`).then(res => {
            this.listData = res.data.data
        })
    }

    @action.bound
    getFlight() {
        const socket = new SockJS('http://192.168.31.100:23499/websocket');
        const stompClient = Stomp.over(socket);
        stompClient.connect({}, () => {
            stompClient.subscribe('/topic/flights', (data: any) => {
                this.flightData.push(...JSON.parse(data.body).payload);
                if(this.flightData.length > 6) {
                    this.flightData.shift();
                }
            });
        });
    }
    @action.bound
    updateTime() {
        const time = new Date();

        const year = time.getFullYear();
        const month = time.getMonth() < 11 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1);
        const day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();

        const hours = time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
        const minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
        const seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();

        const current = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        this.current = current;
    }
}

export default new HomeStore();
